//
//  AppDelegate+XGPush.m
//  KissHealth
//
//  Created by bingo on 2019/1/21.
//  Copyright © 2019 Mark. All rights reserved.
//

#import "AppDelegate+XGPush.h"
#import <QQ_XGPush/XGPush.h>
#import "KHLoginVC.h"

static NSString * const kXGPushAppKey = @"IF21CQF2X51W";

@interface AppDelegate ()<XGPushDelegate>

@end

@implementation AppDelegate (XGPush)

- (void)configXGPushWithLaunchingOptions:(NSDictionary *)launchOptions {
    [[XGPush defaultManager] setEnableDebug:YES];
        
    [[XGPush defaultManager] startXGWithAppID:2200325683 appKey:kXGPushAppKey delegate:self];
    
    // 清除角标
    if ([XGPush defaultManager].xgApplicationBadgeNumber > 0) {
        [[XGPush defaultManager] setXgApplicationBadgeNumber:0];
    }
    
    [[XGPush defaultManager] reportXGNotificationInfo:launchOptions];
}


- (void)setXGApplicationBageNumber:(NSInteger)bageNumber {
    [[XGPush defaultManager] setXgApplicationBadgeNumber:bageNumber];
}

- (void)reportXGNotificationInfo:(nonnull NSDictionary *)info {
    [[XGPush defaultManager] reportXGNotificationInfo:info];
}


#pragma mark - XGPushDelegate
- (void)xgPushDidFinishStart:(BOOL)isSuccess error:(NSError *)error {
    NSLog(@"%s, result %@, error %@", __FUNCTION__, isSuccess?@"OK":@"NO", error);
}

- (void)xgPushDidFinishStop:(BOOL)isSuccess error:(NSError *)error {
    
    
}

- (void)xgPushDidRegisteredDeviceToken:(NSString *)deviceToken error:(NSError *)error {
    NSLog(@"%s, result %@, error %@", __FUNCTION__, error?@"NO":@"OK", error);
}

// iOS 10 新增 API
// iOS 10 会走新 API, iOS 10 以前会走到老 API
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
// App 用户点击通知
// App 用户选择通知中的行为
// App 用户在通知中心清除消息
// 无论本地推送还是远程推送都会走这个回调
- (void)xgPushUserNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
    
    [[XGPush defaultManager] reportXGNotificationResponse:response];
    completionHandler();
    
    [self handleMessageWithUserInfoDict:response.notification.request.content.userInfo];
}

// App 在前台弹通知需要调用这个接口
//- (void)xgPushUserNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
//    [[XGPush defaultManager] reportXGNotificationInfo:notification.request.content.userInfo];
//    completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
//}
#endif
/**
 统一收到通知消息的回调
 @param notification 消息对象
 @param completionHandler 完成回调
 @note SDK 3.2.0+
 */
- (void)xgPushDidReceiveRemoteNotification:(id)notification withCompletionHandler:(void (^)(NSUInteger))completionHandler {
    if ([notification isKindOfClass:[NSDictionary class]]) {
        [[XGPush defaultManager] reportXGNotificationInfo:(NSDictionary *)notification];
        completionHandler(UIBackgroundFetchResultNewData);
    } else if ([notification isKindOfClass:[UNNotification class]]) {
        [[XGPush defaultManager] reportXGNotificationInfo:((UNNotification *)notification).request.content.userInfo];
        completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
        
//        [self handleMessageWithUserInfoDict:((UNNotification *)notification).request.content.userInfo];
    }
    
}

- (void)xgPushDidSetBadge:(BOOL)isSuccess error:(NSError *)error {
    NSLog(@"%s, result %@, error %@", __FUNCTION__, error?@"NO":@"OK", error);
}


#pragma mark - handle

- (void)handleMessageWithUserInfoDict:(NSDictionary *)infoDict {
    
    NSLog(@"%@", infoDict);
    
    if (!infoDict) return;
    NSDictionary *aps = [infoDict objectForKey:@"aps"];
    if (aps.count < 2) return;
    NSString *category = [aps objectForKey:@"category"];
    NSArray *strs = [category componentsSeparatedByString:@"="];
    if (strs.count < 2) return;
    NSString *urlString = [strs lastObject];
    
    NSURL *requestUrl = [NSURL URLWithString:urlString];
    if (![[UIApplication sharedApplication] canOpenURL:requestUrl]) return;
    
    UIViewController *vc = self.window.rootViewController;
    if ([vc isKindOfClass:NSClassFromString(@"KHLoginVC")]) {
        KHLoginVC *loginVC  = (KHLoginVC *)vc;
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:requestUrl];
        [loginVC.webView loadRequest:request];
    }
}

@end
